/**
 * @file models.py
 * @brief SQLAlchemy ORM models for Meeting Management System.
 * @author Ziyi Wang
 *
 * Defines User, Room, Meeting tables and their relationships.
 */
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, Text, Table
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from datetime import datetime

from ..database import Base

/**
 * @brief Association table for many-to-many relationship between users and meetings.
 */
meeting_participants = Table(
    "meeting_participants",
    Base.metadata,
    Column("user_id", Integer, ForeignKey("users.id"), primary_key=True),
    Column("meeting_id", Integer, ForeignKey("meetings.id"), primary_key=True)
)

/**
 * @class User
 * @brief User table model.
 * @property id User ID
 * @property username Username
 * @property email Email address
 * @property hashed_password Hashed password
 * @property full_name Full name
 * @property is_active Active status
 * @property is_admin Admin status
 * @property created_at Creation time
 * @property owned_meetings Meetings created by user
 * @property participated_meetings Meetings user participates in
 */
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    full_name = Column(String)
    is_active = Column(Boolean, default=True)
    is_admin = Column(Boolean, default=False)
    created_at = Column(DateTime, default=func.now())
    
    # 关系
    owned_meetings = relationship("Meeting", back_populates="owner")
    participated_meetings = relationship(
        "Meeting", 
        secondary=meeting_participants,
        back_populates="participants"
    )

/**
 * @class Room
 * @brief Meeting room table model.
 * @property id Room ID
 * @property name Room name
 * @property capacity Capacity
 * @property location Location
 * @property facilities Facilities
 * @property is_active Active status
 * @property created_at Creation time
 * @property meetings Meetings in this room
 */
class Room(Base):
    __tablename__ = "rooms"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True, index=True)
    capacity = Column(Integer)
    location = Column(String)
    facilities = Column(String)  # 设施，如投影仪、白板等
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=func.now())
    
    # 关系
    meetings = relationship("Meeting", back_populates="room")
    
/**
 * @class Meeting
 * @brief Meeting table model.
 * @property id Meeting ID
 * @property title Title
 * @property description Description
 * @property start_time Start time
 * @property end_time End time
 * @property owner_id Owner user ID
 * @property room_id Room ID
 * @property is_cancelled Cancelled status
 * @property created_at Creation time
 * @property updated_at Update time
 * @property owner Owner user
 * @property room Room
 * @property participants Participants
 */
class Meeting(Base):
    __tablename__ = "meetings"
    
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, index=True)
    description = Column(Text)
    start_time = Column(DateTime, index=True)
    end_time = Column(DateTime, index=True)
    owner_id = Column(Integer, ForeignKey("users.id"))
    room_id = Column(Integer, ForeignKey("rooms.id"))
    is_cancelled = Column(Boolean, default=False)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # 关系
    owner = relationship("User", back_populates="owned_meetings")
    room = relationship("Room", back_populates="meetings")
    participants = relationship(
        "User",
        secondary=meeting_participants,
        back_populates="participated_meetings"
    ) 